package com.afeng;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationListener;
import org.springframework.util.StringUtils;

import java.net.InetAddress;

/**
 * 启动器
 * Profile多环境启动方案：
 * 1.直接运行jar,指定uat环境运行[java -jar -Dspring.profiles.active=test boot-simple.jar]
 * 2.Eclipse指定uat环境运行[在StartUpApplication.java启动环境的配置项中添加--spring.profiles.active=uat]
 * 3.直接在StartUpApplication主函数里面配置：
 * SpringApplication app = new SpringApplication(StartUpApplication.class);
 * app.setAdditionalProfiles("uat");   //default dev 或uat
 * app.run(args);
 */
@Slf4j
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class ApiApplication extends SpringBootServletInitializer implements ApplicationListener<WebServerInitializedEvent> {


    public static void main(String[] args) throws Exception {
        SpringApplication.run(ApiApplication.class, args);
    }


    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        try {
            String activeProfiles = StringUtils.arrayToCommaDelimitedString(event.getApplicationContext().getEnvironment().getActiveProfiles());
            activeProfiles = StringUtils.isEmpty(activeProfiles) ? "default" : activeProfiles;
            System.out.println("===========系统启动完成！" + "运行环境:[" + activeProfiles + "] IP:[" + InetAddress.getLocalHost().getHostAddress() + "] PORT:[" + event.getWebServer().getPort() + "]===========");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * web容器中进行部署
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(ApiApplication.class);
    }

}
